package com.cobra.activiti.controller;

import com.cobra.activiti.pojo.param.ProcessSelectListParam;
import com.cobra.activiti.pojo.param.SingleDeploymentIdParam;
import com.cobra.activiti.pojo.param.SingleStrIdParam;
import com.cobra.activiti.pojo.vo.PageVO;
import com.cobra.activiti.pojo.vo.ProcessVO;
import com.cobra.activiti.response.ResultVOUtil;
import com.cobra.activiti.response.ReturnValueLoader;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaobin
 * @date 2020/4/20 0020 14:43
 * @Description 流程 Controller类
 */
@RestController
@Slf4j
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 已部署流程列表
     *
     * @param requestParam
     * @return
     */
    @PostMapping("/selectList")
    public ReturnValueLoader<PageVO<ProcessVO>> getProcessList(//@ApiParam(name = "receiveSwitchUpdateRequestVO", value = "json格式", required = true)
                                                               @Valid @RequestBody ProcessSelectListParam requestParam) {

        PageVO<ProcessVO> result = new PageVO<>();

        Integer current = requestParam.getCurrent();
        Integer queryPage = current - 1;
        Integer size = requestParam.getSize();

        Long count = repositoryService.createProcessDefinitionQuery().count();
        Integer intCount = count.intValue();

        List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionVersion().desc().listPage(queryPage, size);
        List<ProcessVO> processVOList = processDefinitions.stream().map(processDefinition -> {
            ProcessVO process = new ProcessVO();
            process.setId(processDefinition.getId());
            process.setProcessDefinitionKey(processDefinition.getKey());
            process.setName(processDefinition.getName());
            process.setDeploymentId(processDefinition.getDeploymentId());
            process.setDescription(processDefinition.getDescription());
            return process;
        }).collect(Collectors.toList());

        result.setRecords(processVOList);
        result.setTotal(intCount);
        result.setSize(size);
        result.setCurrent(current);

        return ResultVOUtil.success(result);

    }

    /**
     * 删除已部署的流程
     *
     * @param requestParam
     * @return
     */
    @PostMapping("/deleteProcess")
    public ReturnValueLoader deleteProcess(//@ApiParam(name = "receiveSwitchUpdateRequestVO", value = "json格式", required = true)
                                           @Valid @RequestBody SingleDeploymentIdParam requestParam) {

        try {
            repositoryService.deleteDeployment(requestParam.getDeploymentId());
            return ResultVOUtil.success("删除成功！");
        } catch (ActivitiObjectNotFoundException e){
            e.printStackTrace();
            return ResultVOUtil.success("删除的信息不存在！");
        }

    }

}
